package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @BelongsProject: tingshu-parent
 * @BelongsPackage: com.atguigu.tingshu.account.receiver
 * @Author: jiangyi
 * @CreateTime: 2025-10-02  22:38
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class AccountListener {

    @Resource
    private UserAccountService  userAccountService;



    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ACCOUNT,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_ACCOUNT,durable = "true"),
            key = MqConst.ROUTING_ACCOUNT
    ))
    public void updateAccount(Channel channel, Message message, String strUserId) throws IOException {
        long userId = Long.parseLong(strUserId);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 消息确认标识
        try {
            userAccountService.initUserAccount(userId);
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            channel.basicNack(deliveryTag, false, true);

        }

    }

}
